﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SundayWishBLL;
using SundayWishModel.Entities;

public partial class User_privatedishlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ContentPlaceHolder UserMaster = (ContentPlaceHolder)Page.Master.Master.FindControl("ContentPlaceHolder1");
        ((Label)UserMaster.FindControl("lbpagename")).Text = "个人食谱";
        ((HyperLink)UserMaster.FindControl("hlprivatedishlist")).CssClass = "select_page";
        if (!IsPostBack)
        {
            DataBind();
            planlistbind();
        }
    }

    private void DataBind()
    {
        PrivateDishHelper pdh = new PrivateDishHelper();
        if((UserInfo)Session["user"]!=null)
        {
            IList<Dish> dishlist = pdh.GetDishByUser(((UserInfo)Session["user"]).UserId);
            gvPlanlist.DataSource = dishlist;
            gvPlanlist.DataBind();
            dishnumber.InnerText = dishlist.Count.ToString();
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("editData"))
        {
            int DishId = Convert.ToInt32(e.CommandArgument);
            Session["DishId"] = DishId;
            Response.Redirect("editprivatedish.aspx");
        }
        if (e.CommandName.Equals("delData"))
        {
            int DishId = Convert.ToInt32(e.CommandArgument);
            DishHelper dh = new DishHelper();
            Dish dish = dh.GetDish(DishId);
            dh.DeleteDish(dish);
            planlistbind();
        }
        if (e.CommandName.Equals("btnAddToPlan"))
        {
            if (Session["currentplan"] != null)
            {
                int DishId = Convert.ToInt32(e.CommandArgument);
                PlanHelper ph = new PlanHelper();
                if (ph.AddDishToPlan(DishId,
                Convert.ToInt32(Session["currentplan"]))) WebHelper.AlertMsg("添加", "添加成功");
                else WebHelper.AlertMsg("添加失败", "食谱已存在");
                planlistbind();
            }
            else
            {
                clearplanlist();
                WebHelper.AlertMsg("食谱计划", "请选择食谱计划");
            }
        }
        if (e.CommandName.Equals("btnDetail"))
        {
            int DishId = Convert.ToInt32(e.CommandArgument);
            Response.Redirect(DishId.ToString());
        }
        DataBind();
    }


    private void planlistbind()
    {
        if (Session["currentplan"] != null)
        {
            PlanHelper ph = new PlanHelper();
            Plan plan = ph.GetPlan(Convert.ToInt32(Session["currentplan"]));
            if (plan != null)
            {
                gvCurrentPlan.DataSource = plan.Dishes;
                gvCurrentPlan.DataBind();
                lbdate.Text = "当前计划日期：" + plan.SendDate.ToLongDateString();
                lbweek.Text = WebHelper.DateToWeek(plan.SendDate);
            }
            else clearplanlist();
        }
    }

    private void clearplanlist()
    {
        gvCurrentPlan.DataSource = null;
        gvCurrentPlan.DataBind();
        lbdate.Text = "尚未选择食谱计划";
        lbweek.Text = "";
    }
    protected void current_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("delData"))
        {
            if (Session["currentplan"] != null)
            {
                int DishId = Convert.ToInt32(e.CommandArgument);
                PlanHelper ph = new PlanHelper();
                ph.DeleteDishFromPlan(DishId, Convert.ToInt32(Session["currentplan"]));
            }
        }
        planlistbind();
    }
}
